
package com.wyzz.hr.tag;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.UsersAuthority;
import com.wyzz.hr.bo.IHasAuthBO;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.Interpreter;
import com.wyzz.hr.util.InterpreterException;

public class AuthTag extends BodyTagSupport {

	public AuthTag() {
	}

	public int doStartTag() throws JspException {
		if ((prefix == null || prefix.length() == 0)
				&& (auths == null || auths.length() == 0))
			return 0;
		if (prefix != null && prefix.length() > 0) {
			try {
				return authBo.hasJSPAuth(super.pageContext, prefix) ? 1 : 0;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return 0;
		}
		return auths == null || auths.length() <= 0 || hasAuths(auths) ? 1 : 0;
	}

	public boolean hasAuth(String moduleId, String condition[]) {
		UsersAuthority userAuthority;
		if (condition != null && condition.length > 0)
			userAuthority = new UsersAuthority(moduleId,
					new String[] { condition[0] });
		else
			userAuthority = new UsersAuthority(moduleId, new String[0]);
		try {
			return authBo.hasJSPAuth(super.pageContext, userAuthority);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean hasAuths(String authString) {//111
		String authStr;
		authStr = authString.replaceAll(" ", "").toUpperCase();
		authStr = Interpreter.removeBrackets(authStr);
		if (StringUtils.isEmpty(authStr))
			return false;
		String splitAuthStr[];
		try {
			splitAuthStr = Interpreter.seperateauthString(authStr);
			if (splitAuthStr.length == 3) {
				boolean result = hasAuths(splitAuthStr[0]);
				boolean resultRight = hasAuths(splitAuthStr[1]);
				if ("AND".equals(splitAuthStr[2]))
					result = result && resultRight;
				else if ("OR".equals(splitAuthStr[2]))
					result = result || resultRight;
				else
					throw new InterpreterException((new StringBuilder())
							.append("\u903B\u8F91\u8FD0\u7B97\u7B26").append( // 逻辑运算符
									splitAuthStr[2]).append(
									"\u9519\u8BEF\uFF01").toString());// 错误！
				return result;
			}
			if (splitAuthStr.length == 2) {
				boolean result = hasAuths(splitAuthStr[0]);
				if ("NOT".equals(splitAuthStr[1]))
					result = !result;
				else
					throw new InterpreterException((new StringBuilder())
							.append("\u903B\u8F91\u8FD0\u7B97\u7B26").append(
									splitAuthStr[1]).append(
									"\u9519\u8BEF\uFF01").toString());
				return result;
			}
			String authArray[];
			authArray = authStr.split(",");
			if (authArray == null || authArray.length == 0)
				throw new InterpreterException((new StringBuilder()).append(
						"\u5B57\u7B26\u4E32").append(authStr).append(// 字符串
						"\u53C2\u6570\u5F02\u5E38\uFF01").toString());// 参数异常！
			if (authArray.length == 1)
				return hasAuth(authArray[0], new String[0]);
			if (authArray.length == 2)
				return hasAuth(authArray[0], new String[] { authArray[1] });
			try {
				throw new InterpreterException((new StringBuilder()).append(
						"\u5B57\u7B26\u4E32").append(authStr).append(
						"\u53C2\u6570\u8FC7\u591A\uFF01").toString());
			} catch (InterpreterException ie) {
				System.out
						.println((new StringBuilder())
								.append(authStr)
								.append(
										" \u6743\u9650\u516C\u5F0F\u89E3\u6790\u9519\u8BEF: ")
								.append(ie.getMessage()).toString());
			}
		} catch (InterpreterException e) {
			e.printStackTrace();
		}
		return false;
	}

	private List getAuthsList(String auths, String splitString) {
		List resultList = new ArrayList();
		try {
			String auth[] = auths.split(splitString);
			int rowNum = auth.length;
			for (int i = 0; i < rowNum; i++) {
				String intTemp[] = auth[i].trim().split(",");
				if (intTemp.length == 1)
					resultList.add(new UsersAuthority(intTemp[0].trim(),
							new String[0]));
				else if (intTemp.length > 1)
					resultList.add(new UsersAuthority(intTemp[0].trim(),
							new String[] { intTemp[1].trim() }));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getAuths() {
		return auths;
	}

	public void setAuths(String auths) {
		this.auths = auths;
	}

	private static final long serialVersionUID = 0xb3090af3ae46fc20L;
	private String prefix;
	private String auths;
	private static IHasAuthBO authBo = (IHasAuthBO) SpringBeanFactory
			.getBean("hasService");

}
